import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const EditModal		= $Lazy.state(()=>import('./cpt/edit-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '条码号',			name: 'spec_code'},
			{ label: '实验号',			name: 'experiment_num'},
			{ label: '容器名称',			name: 'cn_name' },
			{ label: '容器编号',			name: 'cn_number' },
			{ label: '容器场景',			name: 'pid', 			half:true,	type:'treeSelect',	data:[],	checkable:false,	multiple:false,	loading:true },
			{ label: '科室',				name: 'project_pid', 	half:true,  type: 'select',		idStr: 'uuid' },
			{ label: '岗位',				name: 'project_id', 	half:true, 	type: 'select' },
		],
		// 提交表单列表
		formData : [
			{ label: '容器名称',			name: 'cn_name', 		half:true, 	max: 25,		required:true},
			{ label: '容器编号',			name: 'cn_number', 		half:true,	required:true, max: 10},
			{ label: '容器场景',			name: 'pid', 			half:true,	type:'treeSelect',	data:[],	checkable:false,	multiple:false,	loading:true, required:true },
			// { label: '排序',			name: 'item_sort', 		half:true,	type:'number',		minValue: 1,		value: 1 },
			{ label: '行',				name: 'rows', 			half:true, 	type: 'number', required:true, minValue: 1 },
			{ label: '列',				name: 'columns', 		half:true, 	type: 'number', required:true, minValue: 1 },
			{ label: '最低温度',			name: 'start_temp', 	half:true,	type: 'number', required:true, max: 10 },
			{ label: '最高温度',			name: 'end_temp', 		half:true,	type: 'number', max: 10 },
			// { label: '科室',				name: 'project_pid', 	half:true,  type: 'select',		idStr: 'uuid',	onChange:({ value })=> this.getJobs(value) },
			{ label: '科室',				name: 'project_pid', 	half:true,  type: 'select',		idStr: 'uuid' },
			{ label: '岗位',				name: 'project_id', 	half:true, 	type: 'select' },
			{ label: '保存时间(天)',		name: 'keep_time', 		half:true,	type: 'number', required:true, minValue: 1},
			{ label: '备注',			name: 'remark', 		full:true,	type: 'textarea' },
		]
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
	
		$cache.then( async f => {
			// const data = await f.getDicCache({ code:90000 })
			// // $fn.setRow( forms, 'project_name', await f.getCache({ api:'shi_yan_ke_shi' }) )
			// // 容器场景
			// $fn.setRow( forms, 'item_code', data )
			// $fn.setRow( formData, 'item_code', data )
			
			// 岗位
			const projectId = await f.getCache({ api: 'gang_wei' })
			$fn.setRow(formData, 'project_id',  projectId)
			$fn.setRow(forms, 'project_id',  projectId)
			// 科室
			const projectPid = await f.getCache({ api: 'ke_shi' })
			$fn.setRow(formData, 'project_pid', projectPid)
			$fn.setRow(forms, 'project_pid', projectPid)
			
			// 上级名称
			const _pid = await f.getCache({ api:'sp-container-lis/select', param: {level: 1},  refresh:true })
			const pid = await f.getCache({ api:'sp-container-lis/select', refresh:true })
			$fn.setRow(forms, 'pid', pid)
			$fn.setRow(formData, 'pid', _pid)
			
			this.setState({ forms, formData })			
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'sp-container-item/index')
	
	// 表单
	cols = [
		{ title: '容器名称', 		dataIndex: 'cn_name', 				width:200 },
		{ title: '编号', 			dataIndex: 'cn_number', 			...$fn.table.spec_code },
		{ title: '容器场景', 		dataIndex: 'item_path', 			width:150 },
		{ title: '标本容量', 		dataIndex: 'spec_count', 			width:80 },
		{ title: '行', 				dataIndex: 'rows', 					width:50 },
		{ title: '列', 				dataIndex: 'columns', 				width:50 },
		{ title: '最低温度', 		dataIndex: 'start_temp', 			...$fn.table.num, render: text => text + ' ℃' },
		{ title: '最高温度', 		dataIndex: 'end_temp', 				...$fn.table.num, render: text => text + ' ℃' },
		{ title: '保存时间(天)', 	dataIndex: 'keep_time', 			width:100 },
		{ title: '已存标本量', 		dataIndex: 'spec_num', 				width:150 },
		{ title: '科室', 			dataIndex: 'parent_project_name', 	width:100 },
		{ title: '岗位', 			dataIndex: 'project_name', 			width:100 },
		{ title: '备注', 			dataIndex: 'remark', 				width:200 },
		{ title: '创建时间', 		dataIndex: 'created_at', 			...$fn.table.time },
		TableControls.controls.call(this,{ width: 130, align:'left' })
	]
	
	// 表格操作按钮
	table_controls = (text,rows) => {
		return [
			TableControls.edit.call(this, null, null, { rows, 
				click: ()=> {
					const { formData } = this.state
					$fn.disabledRows(formData, ['rows', 'columns', 'keep_time'], true)
					this.setState({formData})
				}
			}),		//编辑
			TableControls.pushTo.call(this, '查看', `container-modal?uuid=${rows.uuid}` ),		// 查看
		]
	}
	
	onDel = uuid => Confirm.call(this, '删除', 'sp-container-item/delete', { param: [uuid], idStr:'uuids', isKeys: !uuid })
	
	// 操作按钮
	controls = () => {
		return [
			// 新增
			Controls.add.call(this, {
				click: () => {
					const {formData} = this.state
					$fn.disabledRows(formData, ['rows', 'columns', 'keep_time'], false)
					this.setState({formData})
				}
			}),
			// 删除
			this.onDel(),
			// 设置温度
			Controls.click.call(this, '设置温度', () => {
				this.editModal.open({uuids: this.state.selectedKeys}, true)
			}, {isKeys: !this.state.selectedKeys.length}),
		]
	}
	
	// // 获取岗位
	// getJobs = value => {
	// 	window.$http.submit(null,'project-team/jobselect', { param: { uuid: value }, isPull:true }).then(data=>{
	// 		const { formData } = this.state
	// 		if($fn.hasArray(data)){
	// 			$cache.then( async f => {
	// 				// 岗位
	// 				$fn.setRow(formData, 'project_id', data)
	// 				$fn.getRow(formData,'project_id').p = '请选择岗位'
	// 				this.setState({ formData })
	// 			})
	// 		}
	// 	})
	// }
	
	render(){
		return (
			<>
				<Page title='存放列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { hasCurrent: false, isInit: window.isInit }) }
				</Page>
				{/* 添加 编辑 */}
				{AddEditModal.call(this,{title:'存放列表', labelWidth:'s6', width:800, api:'sp-container-item/add', editApi:'sp-container-item/edit'})}
				{/* 设置温度 */}
				<EditModal onRef={ref => this.editModal = ref} onRefresh={this.fetch} api='sp-container-item/setting' />
			</>
		)
	}
}

export default Index